using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Printing;
using System.Text;
using System.Windows.Forms;
using PrintTray.Properties;
using System.IO;
using System.Threading;

namespace PrintTray
{
    public partial class Manager : Component
    {
        ControlContainer container = new ControlContainer();


        private MainForm form;


        public Manager(MainForm form)
        {
            //form.Container.Add(this);

            InitializeComponent();

            Thread.CurrentThread.Name = "UI Thread";

            this.form = form;

            contextMenuStrip.ItemClicked += new ToolStripItemClickedEventHandler(contextMenuStrip_ItemClicked);

            notifyIcon = new NotifyIcon(container);

            notifyIcon.Icon = Resources.printer;

            notifyIcon.Text = "PrintTray";
            notifyIcon.Visible = true;
            notifyIcon.DoubleClick += new EventHandler(notifyIcon_DoubleClick);

            notifyIcon.ContextMenuStrip = contextMenuStrip;

            Settings.Default.SettingsSaving += new System.Configuration.SettingsSavingEventHandler(Default_SettingsSaving);

            SetupWatcher();
        }

        private void SetupWatcher()
        {
            if (Settings.Default.UserSettings != null)
            {
                if (!String.IsNullOrEmpty(Settings.Default.UserSettings.PathToWatch))
                {
                    fileSystemWatcher.Path = Settings.Default.UserSettings.PathToWatch;
                    fileSystemWatcher.EnableRaisingEvents = true;
                    fileSystemWatcher.Created += new System.IO.FileSystemEventHandler(fileSystemWatcher_Created);
                    //fileSystemWatcher.Filter = Settings.Default.UserSettings.MonitoredExtensions;
                }
            }
        }

        void Default_SettingsSaving(object sender, CancelEventArgs e)
        {
            SetupWatcher();
            ShowBalloon(String.Format(
    "Watching directory: {0}", Settings.Default.UserSettings.PathToWatch), ToolTipIcon.Info);
        }

        void fileSystemWatcher_Created(object sender, System.IO.FileSystemEventArgs e)
        {

            try
            {
                PrintDocument doc = new PrintDocument();

                doc.PrinterSettings = Settings.Default.UserSettings.PrintSettings;
                doc.DocumentName = e.Name;

                doc.PrintPage +=
                    new PrintPageEventHandler(
                        delegate(object printSender, PrintPageEventArgs printEventArgs)
                        {
                            using (Image img = Image.FromFile(e.FullPath))
                            {
                                printEventArgs.Graphics.DrawImage(img, new Point(0, 0));
                                printEventArgs.HasMorePages = false;
                                string log = String.Format(
                                    "{0}: Sent to printer: {1}", Thread.CurrentThread.Name, e.Name);
                                ShowBalloon(log, ToolTipIcon.Info);
                                form.AddLogRecord(log);
                            }
                        });

                doc.Print();


            }
            catch (Exception ex)
            {
                ShowBalloon(ex.ToString(), ToolTipIcon.Error);
            }
        }

        private void ShowBalloon(string text, ToolTipIcon icon)
        {
            notifyIcon.BalloonTipText = text;
            notifyIcon.BalloonTipIcon = icon;
            notifyIcon.BalloonTipTitle = "PrintTray";
            notifyIcon.Visible = true;
            notifyIcon.ShowBalloonTip(2000);
        }

        private void contextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem == exitToolStripMenuItem)
            {
                Application.Exit();
            }
            if (e.ClickedItem == openToolStripMenuItem)
            {
                form.Show();
            }
        }
        private void notifyIcon_DoubleClick(object sender, EventArgs e)
        {
            form.Show();
        }
    }
}
